-module(octree).
-export([build_static/3]).

%% @spec build_static(BBoxes::[bbox()], Min::float(), Filler::fun(bbox())) -> [bbox()]
%% @doc Construye un octree estatico, sin nodos innecesarios
build_static([], _, _) ->
    [];
build_static([Box = {{vec3, X, Y, Z}, W} | T], Min, Filler) ->
    case W =< Min of
        true ->
            Data = Filler(Box),
            case Data of
                empty ->
                    build_static(T, Min, Filler);
                _ ->
                    [ {leaf, Box, Data} | build_static(T, Min, Filler) ]
            end;
        _ ->
            Nw = W / 2,
            Xmid = X + Nw,
            Ymid = Y + Nw,
            Zmid = Z + Nw,
            Hijos = [{{vec3, X, Y, Z}, Nw},
                     {{vec3, Xmid, Y, Z}, Nw},
                     {{vec3, X, Y, Zmid}, Nw},
                     {{vec3, Xmid, Y, Zmid}, Nw},
                     {{vec3, X, Ymid, Z}, Nw},
                     {{vec3, Xmid, Ymid, Z}, Nw},
                     {{vec3, X, Ymid, Zmid}, Nw},
                     {{vec3, Xmid, Ymid, Zmid}, Nw}],
            DatosHijos = build_static(Hijos, Min, Filler),
            case DatosHijos of
                [] ->
                    build_static(T, Min, Filler);
                _ ->
                    [ {node, Box, DatosHijos} | build_static(T, Min, Filler) ]
            end
    end.
